const user = require("../common/api/user.js")

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}


const generateParams = objectParams => {
  let params = "?";
  for (const key in objectParams) {
    if (Object.hasOwnProperty.call(objectParams, key)) {
      const element = objectParams[key];
      params = params + key + "=" + element + "&"
    }
  }
  params = params.substring(0, params.length - 1)
  return params;
}

// 消息提示框封装工具
const wxShowToastUtil = function (msg, icon) {
  var msgtext = JSON.stringify(msg)
  uni.showToast({
    title: msgtext.substring(0, 30),
    icon: icon == null ? 'none' : icon,
    duration: 2000,
    mask: true,
  });
}


// 消息提示框封装工具
const wxShowLoadingtUtil = function (msg) {
  var msgtext = JSON.stringify(msg)
  uni.showLoading({
    title: msgtext.substring(0, 30),
    mask: true,
  });

}

// 检查登录状态
const loginStatus = function () {
  var token = uni.getStorageSync("token");
  user.loginStatusUsingGET(null, token)
    .then(res => {
      if (res.data.code == 200) {
        console.log(res)
      } else {
        this.wxShowToastUtil("登录状态已过期")
        uni.showModal({
          title: '您还未登录',
          content: '是否现在去登录?',
          showCancel: true,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (result) => {
            if (result.confirm) {
              uni.navigateTo({
								url: '/pages/index/index',
                success: (result) => {
                  console.log(result)
                },
                fail: function (res) {
					
                  console.log(res)
                }

              });
            }
          },
          fail: () => {},
          complete: () => {}
        });


      }
    })
    .catch(res => {
      this.wxShowToastUtil("网络异常")

    })

}

// 检查认证状态
const authStatus = function () {
  var token = uni.getStorageSync("token");
  user.guetAuthUsingGET(null, token)
    .then(res => {
      if (res.data.code == 200) {
        console.log(res)
      } else {
        this.wxShowToastUtil("还未进行教务认证")
        uni.showModal({
          title: '您还没有认证',
          content: '是否现在要去认证?',
          showCancel: true,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (result) => {
            if (result.confirm) {
              uni.navigateTo({
                // 要改成认证界面
								url: '/pages/schoolAccreditation/schoolAccreditation',
                success: (result) => {
                  console.log(result)
                },
                fail: function (res) {
                  console.log(res)
                }

              });
            }
          },
          fail: () => {},
          complete: () => {}
        });


      }
    })
    .catch(res => {
      this.wxShowToastUtil("网络异常")

    })

}


module.exports = {
  formatTime,
  generateParams,
  wxShowToastUtil,
  wxShowLoadingtUtil,
  loginStatus,
  authStatus
}